package com.lixin.web.webSocket.impl;

import com.lixin.model.notify.NotifyRes;
import com.lixin.model.response.Response;
import com.lixin.redis.ListRedisUtil;
import com.lixin.redis.RedisKey;
import com.lixin.service.IGroupChatMessageService;
import com.lixin.service.IMessagesService;
import com.lixin.web.netty.UserSessionManager;
import com.lixin.web.webSocket.NotificationManager;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * Description:
 * Copyright:   Copyright (c)2023
 * Company:     sci
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2023-08-03 21:26:00
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2023-08-03     张李鑫                     1.0         1.0 Version
 */
@Service("NotificationManagerImpl")
public class NotificationManagerImpl implements NotificationManager {

    @Autowired
    private ListRedisUtil listRedisUtil;

    @Autowired
    private UserSessionManager userSessionManager;
    @Override
    @Async //小项目异步操作即可
    public <T> void sendNotification(NotifyRes<T> notify, Integer recipientId) {
        Channel channel = userSessionManager.getChannelByUserId(recipientId);
        if (channel != null && channel.isActive()) {
            //用户在线
            channel.writeAndFlush(Response.success(notify));
            return;
        }
        //用户不在线
        listRedisUtil.lPush(RedisKey.getKey(RedisKey.USER_NOTIFY, recipientId), notify);
    }
}
